﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using MONTRASEC_test.ControlsNFP;
using MONTRASEC_test.Classes;
namespace MONTRASEC_test.EUMonitoringCentre
{
  public partial class B_1_Events : System.Web.UI.Page
  {
   
    protected void Page_Load_from_template(object sender, EventArgs e)
    {
      if (!IsPostBack)
      {
        Session["CurrentANewEntryID"] = null;
      }
      mytemplateHelperTopButtons.ExportExcelButton.Visible = true;
      myCurrentTable = myDVB1Events;
      Subscribe(myGVANewEntry);
      Subscribe(mytemplateHelperTopButtons);
      Subscribe(myDVB1Events);
    }

    // given a Gridview, subscribe to its SelectedIndexChangedHandler event
    public void Subscribe(MONTRASEC_test.ControlsEUMonitoringCentre.wucDGANewEntryEU theDGANewEntry)
    {
      theDGANewEntry.SelectedIndexChanged += new MONTRASEC_test.ControlsEUMonitoringCentre.wucDGANewEntryEU.SelectedIndexChangedHandler(gvSelectedIndexHasChanged);
    }
    public void gvSelectedIndexHasChanged(object sender, EventArgs e)
    {
      
    
      myCurrentTable.dv.DataBind();

      if (myCurrentTable.dv.DataItemCount == 0)
      {
        StatusDVMode = "You can 'Create a new record' for the selected Entry";
      }
      else
      {
        StatusDVMode = UtilHelper.getTextForDVMode(myCurrentTable.dv.CurrentMode);
      }
      myCurrentTable.dv.ChangeMode(DetailsViewMode.ReadOnly);

      UpdatePageBasedOnStatusOfControls();
    }

    //put here all controls that must update their content (eg) when a button has been clicked
    private void UpdatePageBasedOnStatusOfControls()
    {
      //myCurrentTable.LDVMode.Text = StatusDVMode;
      myCurrentTable.LbTotalRecords.Text = " - New Entry ID: " + UtilHelper.getSelectedRecord(Session["CurrentANewEntryID"]);

    }


    public void buCreateNewRecordIsPushed(object sender, EventArgs e)
    {
      myCurrentTable.dv.ChangeMode(DetailsViewMode.Insert);
      StatusDVMode = UtilHelper.getTextForDVMode(myCurrentTable.dv.CurrentMode);
      UpdatePageBasedOnStatusOfControls();
    }


    // given a TemplateHelperTopButtons, subscribe to its buCreateNewRecord_Clicked event
    public void Subscribe(wucTemplateHelperTopButtons theTemplateHelperTopButtons)
    {
      // don't change this!
      theTemplateHelperTopButtons.CreateNewRecord_Clicked += new wucTemplateHelperTopButtons.buCreateNewRecord_ClickHandler(buCreateNewRecordIsPushed);
      theTemplateHelperTopButtons.ExportExcel_Clicked += new wucTemplateHelperTopButtons.buExportExcel_ClickHandler(buExportExcelIsPushed);
    }

    public void buExportExcelIsPushed(object sender, EventArgs e)
    {
      ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", "window.open( '../Reporting/1GraphForm.aspx', null, 'status=yes,titlebar=no,resizable=yes,toolbar=no,menubar=no,location=no' );", true);
    }

    // given a DetailedView, subscribe to its dvANewEntry_ModeChanged event
    public void Subscribe(wucDVB1Events theDetailsView)
    {
      // don't change this!
      theDetailsView.DetailsView_ModeChanged += new wucDVB1Events.detailsView_ModeChangedHandler(detailsView_ModeChanged);
      theDetailsView.DetailsView_ItemInserted += new wucDVB1Events.detailsView_ItemInsertedHandler(detailsView_ItemInserted);
      theDetailsView.DetailsView_ItemUpdated += new wucDVB1Events.detailsView_ItemUpdatedHandler(detailsView_ItemUpdated);
      theDetailsView.DetailsView_ItemDeleted += new wucDVB1Events.detailsView_ItemDeletedHandler(detailsView_ItemDeleted);
    }
    public void detailsView_ModeChanged(object sender, EventArgs e)
    {
      StatusDVMode = UtilHelper.getTextForDVMode(myCurrentTable.dv.CurrentMode);
      UpdatePageBasedOnStatusOfControls();
    }
    public void detailsView_ItemDeleted(object sender, EventArgs e)
    {
      StatusDVMode = "Record has been deleted";

      UpdatePageBasedOnStatusOfControls();
      Response.Redirect(Request.Url.LocalPath);
    }
    public void detailsView_ItemUpdated(object sender, EventArgs e)
    {
      StatusDVMode = "Record has been Updated";

      UpdatePageBasedOnStatusOfControls();
      Response.Redirect(Request.Url.LocalPath);
    }
    public void detailsView_ItemInserted(object sender, EventArgs e)
    {
      StatusDVMode = "Record has been inserted";

      UpdatePageBasedOnStatusOfControls();
      Response.Redirect(Request.Url.LocalPath);
    }

    String StatusDVMode = "";
    internal static wucDVB1Events myCurrentTable;
  }
}
